﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.IO;
using TanashCMS.Business;

public partial class manager_settings_TemplatePreview : CMSManagementPageBase
{
    string templatePath = "";
    XslCompiledTransform xPageTransform = null;
    StringBuilder sb = new StringBuilder();

    protected override void OnPreInit(EventArgs e)
    {
        base.OnPreInit(e);
        this.MasterPageFile = "";
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Profile.WebsiteID != 0)
            mWebsite = (TanashWebsite)Configuration.GetCurrentSession().Get(typeof (TanashWebsite), Profile.WebsiteID);

        Response.Clear();
        if (Request.QueryString["tp"] == null)
        {
            Response.Write("no template");
            return;
        }
        templatePath = Server.MapPath(Path.Combine(mWebsite.TemplatesFolder, Request.QueryString["tp"]));

        if (Request.QueryString["temp"] != null && File.Exists(templatePath + @"\template_temp.xsl"))
            templatePath += @"\template_temp.xsl";
        else
            templatePath += @"\template.xsl";

        //context.Response.Write("website is " + website.Title + "<br/>");
        string xmlUri = Web2Xml.getWebsiteXml(((CMSPageBase)Page).Website.ID);
        XmlDocument xDoc = new XmlDocument();
        xDoc.Load(Server.MapPath(xmlUri));
        XPathNavigator xNav = xDoc.CreateNavigator();
        XsltArgumentList xArguments = new XsltArgumentList();
        
        StringWriter sw = new StringWriter(sb);

        XsltSettings settings = new XsltSettings(false, true);
        xPageTransform = new XslCompiledTransform();
        xPageTransform.Load(templatePath, settings, new XmlUrlResolver());

        xPageTransform.Transform(xNav, xArguments, sw);
        sw.Close();


    }

    protected override void Render(HtmlTextWriter writer)
    {
        //base.Render(writer);
        Response.Write(Server.HtmlDecode(Server.HtmlDecode(sb.ToString())));
    }
}
